package Function;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import javazoom.jl.player.Player;

public class FCSound {
	private String[][] musicurl = { { "./material/sound/start0.mp3", "./material/sound/start1.mp3" }, // 游戏开始 0
			{ "./material/sound/loading.mp3" }, // 开房、存档等准备音乐 1
			{ "./material/sound/town0.mp3", "./material/sound/town1.mp3" }, // 拉多镇音乐2
			{ "./material/sound/bar0.mp3", "./material/sound/bar1.mp3" }, // 拉多镇酒吧音乐3
			{ "./material/sound/map1.mp3" }, // 没有战车时大地图音乐4
			{ "./material/sound/start1.mp3" }, // 返回游戏主菜单5
			{ "./material/sound/guochang.mp3" }, // 地图转换时的音乐6
			{ "./material/sound/DongXue1.mp3" }, // 进洞穴1的音乐7
			{ "./material/sound/zhandouqiehuan1.mp3" }, // 普通战斗进场音乐8
			{ "./material/sound/zhandouyinyue1.mp3" }, // 普通战斗背景音乐9
			{ "./material/sound/zhandouhuosheng.mp3" }, // 10
			{ "./material/sound/drop.mp3" }, // 掉落音乐11
			{ "./material/sound/libiequ.mp3" }, // 离别曲12
			{ "./material/sound/yuanwuqu.mp3" }, // 圆舞曲13
			{ "./material/sound/anjian.mp3" }, // 按键音效14
			{ "./material/sound/music1.mp3" }, // 游戏开始剧情音乐15
			{ "./material/sound/opendoor.mp3" }, // 开关门的声音16
			{ "./material/sound/music2.mp3" }, // 拉多镇关口房子音乐17
			{ "./material/sound/music3.mp3" }, // 普通战斗获胜音乐18
			{ "./material/sound/music4.mp3" }, // 升级音乐19
			{ "./material/sound/music5.mp3" }, // 打开箱子音效20
			{ "./material/sound/music6.mp3" }, // 上下坦克音效21
			{ "./material/sound/music7.mp3" }, // 中奖音效22
			{ "./material/sound/battlemusic1.mp3" }, // 弹弓打中怪音效23
			{ "./material/sound/battlemusic2.mp3" }, // 弹弓发射音效24
			{ "./material/sound/battlemusic3.mp3" }, // 炮弹发射音效25
			{ "./material/sound/battlemusic4.mp3" }, // 炮弹爆炸音效1-26
			{ "./material/sound/battlemusic5.mp3" }, // 怪物死亡音效1-27
			{ "./material/sound/battlemusic6.mp3" }, // 短枪音效1-28
			{ "./material/sound/battlemusic7.mp3" },// 巨蚁攻击音效29
			{ "./material/sound/Caravasso.mp3" }// 巨蚁攻击音效30
	};
	private Player play;
	private Player play2;
	private int num = -1;
	private int playnum = 0;
	private int effnum = -1;
	private playBackgroundMusic qwe;
	private playEffectSound asd;

	public FCSound() {
		qwe = new playBackgroundMusic();
		qwe.start();
	}

	// 设置音效
	public void setEffectSound(int num) {
		this.effnum = num;
		asd = new playEffectSound();
		asd.start();
	}

	/*
	 * 根据地图播放背景音乐
	 */
	public void setBackgroundMusicOfMapName(String mapname) {
		if (mapname.indexOf("LaDuo") > -1 && mapname.indexOf("Bar") < 0)
			setBackgroundMusic(2);
		else if (mapname.indexOf("LaDuo") > -1 && mapname.indexOf("Bar") > -1)
			setBackgroundMusic(3);
		else if (mapname.indexOf("world") > -1)
			setBackgroundMusic(4);
		else if (mapname.indexOf("DongXue") > -1)
			setBackgroundMusic(7);
		else
			setBackgroundMusic(4);
	}

	/*
	 * 设置背景音乐曲目
	 */
	public void setBackgroundMusic(int num) {
		if (this.num != num) {
			qwe.stop();
			this.num = num;
			qwe = new playBackgroundMusic();
			playnum = 0;
			qwe.start();
		}
	}

	private boolean isMusicEnd = false;

	// 播放背景音乐
	private class playBackgroundMusic extends Thread {
		@Override
		public void run() {
			try {
				while (true) {
					if (num < 0)
						isMusicEnd = false;
					else
						isMusicEnd = true;
					if (isMusicEnd) {
						play = new Player(new BufferedInputStream(
								new FileInputStream(new File((playnum == 0) ? musicurl[num][0] : musicurl[num][1]))));
						play.play();
						playnum = (musicurl[num].length > 1) ? 1 : 0;
					}
					Thread.sleep(50);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	// 播放音效
	private class playEffectSound extends Thread {
		@Override
		public void run() {
			try {
				if (effnum > -1) {
					play2 = new Player(new BufferedInputStream(new FileInputStream(new File(musicurl[effnum][0]))));
					play2.play();
					stop();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	// 获取当前背景音乐编号
	public int getNowMusicNum() {
		return num;
	}
}
